package leetCode.hw.string;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class HJ19Solution implements HJ19{


    @Override
    public List<String> statErrorLog(List<String> infos) {
        List<String> result = new ArrayList<>();
        LinkedHashMap<String,Integer> treeMap = new LinkedHashMap<>();
        for(String log:infos) {
            String[] one = log.split(" ");
            String[] names = one[0].split("\\\\");
            String name = names[names.length-1];
            if(name.length()>16){
                name = name.substring(name.length()-16);
            }
            if(!treeMap.containsKey(name+" "+one[1]))
                treeMap.put(name+" "+one[1],1);
            else
                treeMap.put(name+" "+one[1],treeMap.get(name+" "+one[1])+1);
        }
        for(Map.Entry<String,Integer> entry: treeMap.entrySet()){
            result.add(entry.getKey()+" "+entry.getValue());
        }
        while(result.size()>8) {
            result.remove(0);
        }
        return result;
    }
}
